﻿using Dapper;
using Microsoft.Extensions.Configuration;
using PcProject.Domain.IRepository;
using PcProject.Domain.Models.Entities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PcProject.Infrastructure.Repository
{
    public class UserRepository : DapperRepositoryBase<Users>, IUserRepository
    {
        //构造函数依赖注入
        public IConfiguration _configuration;
        //函数依赖注入
        public UserRepository(IConfiguration configuration)
        {
            this._configuration = configuration;
            //连接字符串
            this.ConnStr = _configuration.GetConnectionString("PcMiniConnStr");
            SimpleCRUD.SetDialect(SimpleCRUD.Dialect.MySQL);
            Conn = new MySql.Data.MySqlClient.MySqlConnection(this.ConnStr);
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="LoginName"></param>
        /// <param name="LoginPwd"></param>
        /// <returns></returns>
        public int LoginAsync(string LoginName,string LoginPwd)
        {
            string sql = $"select * from Users where LoginName='{LoginName}' && LoginPwd='{LoginPwd}'";
            return Conn.Query<int>(sql).FirstOrDefault();
        }

        /// <summary>
        /// 根据Id查询用户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public List<WeiXinLoginResult> GetUsers(string id)
        {
            string sql = $"select * from WeiXinLoginResult where 1=1";
            if (!string.IsNullOrEmpty(id))
            {
                sql += $" and OpenId like '%{id}%'";
            }
            return Conn.Query<WeiXinLoginResult>(sql).ToList();
        }

    }
}
